package com.desksoft.ecommerce.util.html.element;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Element;

import com.desksoft.ecommerce.util.html.PageXml;
import com.desksoft.ecommerce.util.html.element.data.validata.ValidataElement;
import com.desksoft.ecommerce.util.html.element.data.validata.ValidataInterfaceElement;
import com.desksoft.ecommerce.util.html.element.data.validata.ValidataRegularElement;
import com.desksoft.ecommerce.util.html.element.data.validata.ValidataSimpleElement;
import com.desksoft.ecommerce.vo.KeyAndValue;
import com.desksoft.utility.query.AddToQuerySql;
import com.desksoft.utility.query.FieldSymbol;

/***************************************
 * 版权所有：杭州迪科软件 保留所有权利 
 * 创建日期: 
 * 创建作者：luoy 
 * 文件名称：页面元素，输入元素父类 
 * 版本： 
 * 功能： 
 * 最后修改时间： 
 * 修改记录：
 *****************************************/
public class PageDataElement extends PageElement {

	protected String name; // name属性的值、字段名
	protected String dataType;		//	 数据类型，标准数据类型
	
	protected FieldSymbol symbol = FieldSymbol.eq;	//	过滤条件(默认为EQ)
	
	protected boolean readonly = false;		//	是否只读(默认为否)
	
	protected String disabled;		//	是否可用
	
	protected ValidataElement[] validatas;	//	验证数组
	protected AddToQuerySql querySql;
	
	/**
	 * 事件处理
	 */
	protected List<PageEventElement> eventL;
	
	protected PageXml pageXml;
	
	public PageDataElement(Element element,PageXml pageXml) {
		super(element);
		
		this.pageXml = pageXml;
		this.name = element.attributeValue("name");
		this.dataType = element.attributeValue("dataType");
		
		if ("true".equals(element.attributeValue("readonly"))){
			this.readonly = true;
		}
		
		this.disabled = element.attributeValue("disabled");
		
		String symbolstr = element.attributeValue("symbol");
		if (symbolstr!=null&&FieldSymbol.valueOf(symbolstr)!=null){
			symbol = FieldSymbol.valueOf(symbolstr);
		}
		
		Element validataElement = element.element("validataElement");
		if (validataElement != null){
			this.validatas = parseValidata(validataElement,pageXml);
		}
		
		Element querySqlElement = element.element("querySql");
		if (querySqlElement != null){
			this.querySql = parseQuerySql(querySqlElement);
		}
		
		Element listenerElement = element.element("listener");
		if (listenerElement != null){
			this.eventL = this.parsePageEvent(listenerElement);
			this.pageXml.getEventL().addAll(this.eventL);
		}
	}
	
	@Override
	public String toHtml() {
		StringBuffer html = new StringBuffer();
		html.append("<"+this.target+" id='"+this.getId()+"' class='"+this.getCssClass()+"' style='" + this.style + "' name='"+this.getName()+"' datatype='" + this.dataType + "'");
		html.append(" symbol='"+symbol.toString()+"' ");
		if (this.readonly){
			html.append(" readonly='readonly' ");
		}
		if ("disabled".equals(this.disabled) || "true".equals(this.disabled)){
			html.append(" disabled='disabled' ");
		}
		if (this.querySql != null){
			if (this.querySql.getSelect() != null){
				html.append(" addto_select=\"" + this.querySql.getSelect() + "\" ");
			}
			if (this.querySql.getFrom() != null){
				html.append(" addto_from=\"" + this.querySql.getFrom() + "\" ");
			}
			if (this.querySql.getWhere() != null){
				html.append(" addto_where=\"" + this.querySql.getWhere() + "\" ");
			}
		}
		html.append(appendAttr());
		html.append(">");
		html.append(appendOptions());
		html.append("</"+this.target+">");
		
		return html.toString();
	}
	
	/**
	 * 留给子类实现
	 * @return
	 */
	public String appendOptions(){
		return "";
	}
	
	/**
	 * 留给子类实现
	 * @return
	 */
	public String appendAttr(){
		return "";
	}
	
	/**
	 * 解析事件元素
	 */
	public List<PageEventElement> parsePageEvent(Element listenerElmt){
		
		List<Element> eventElmtL = listenerElmt.elements("event");
		List<PageEventElement> eventL = new ArrayList<PageEventElement>(eventElmtL.size());

		for (Element elmt : eventElmtL){
			Element funElmt = elmt.element("function");
			String type = elmt.attributeValue("type"), fun = funElmt.getText();
			eventL.add(new PageEventElement(this.id, type, fun));
		}
		
		return eventL;
	}
	
	/**
	 * 解析追加Sql元素
	 */
	public AddToQuerySql parseQuerySql(Element elmt){
		if (elmt == null){
			return null;
		}

		String slt = null, from = null, where = null;
		Element elmtSlt = elmt.element("select");
		if (elmtSlt != null){
			slt = elmtSlt.getText();
		}
		Element elmtFrom = elmt.element("from");
		if (elmtFrom != null){
			from = elmtFrom.getText();
		}
		Element elmtWhere = elmt.element("where");
		if (elmtWhere != null){
			where = this.pageXml.getVarCollection().replaceSql(elmtWhere.getText().replaceAll("\n", "").replaceAll("\t", " "));
		}
		
		return new AddToQuerySql(slt, from, where);
	}
	
	/**
	 * 解析验证元素
	 */
	public ValidataElement[] parseValidata(Element element,PageXml pageXml){
		List<Element> valiElmtL = element.elements("validata");
		ValidataElement[] validataA = new ValidataElement[valiElmtL.size()];
	
		ValidataElement validata;
		int i = 0;
		String code,regular;
		for (Element elmt : valiElmtL){
			code = elmt.attributeValue("code");
			regular = elmt.attributeValue("regular");
			if (code != null){
				validata = new ValidataInterfaceElement(elmt);
			}else if (regular != null){
				validata = new ValidataRegularElement(elmt);
			}else{
				validata = new ValidataSimpleElement(elmt, pageXml);
			}
			validata.setInputElementId(this.getId());
			validataA[i++] = validata;
			pageXml.getValidataL().add(validata);
		}
		
		return validataA;
	}
	
	/**
	 * 获取option键值对列表，有些可能会用到
	 * @param	element		父元素
	 * @return
	 */
	public List<KeyAndValue<String,String>> getKeyAndValueList(Element element){
		
		List<Element> elementL = element.elements("option");
		List<KeyAndValue<String,String>> kvL = new ArrayList<KeyAndValue<String,String>>(elementL.size());
		
		KeyAndValue kv;
		String value,text;
		for (Element e:elementL){
			value = e.attributeValue("value");
			text = e.getText();
			kv = new KeyAndValue<String,String>();
			kv.setKey(value);
			kv.setValue(text);
			kvL.add(kv);
		}
		
		return kvL;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public FieldSymbol getSymbol() {
		return symbol;
	}

	public void setSymbol(FieldSymbol symbol) {
		this.symbol = symbol;
	}

	public boolean isReadonly() {
		return readonly;
	}

	public void setReadonly(boolean readonly) {
		this.readonly = readonly;
	}

	public ValidataElement[] getValidatas() {
		return validatas;
	}

	public void setValidatas(ValidataElement[] validatas) {
		this.validatas = validatas;
	}

	public AddToQuerySql getQuerySql() {
		return querySql;
	}

	public void setQuerySql(AddToQuerySql querySql) {
		this.querySql = querySql;
	}

	public List<PageEventElement> getEventL() {
		return eventL;
	}

	public void setEventL(List<PageEventElement> eventL) {
		this.eventL = eventL;
	}

	public String getDisabled() {
		return disabled;
	}

	public void setDisabled(String disabled) {
		this.disabled = disabled;
	}

}
